<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>

<%= form_for(
      @product,
      html: { :multipart => true },
      role: 'form') do |f| %>

  <%= render partial: 'admin/product/images' %>

  <script type="text/javascript">
    $(function() {
      <% if @product.images && @product.images.length > 0 %>
        <% @product.images.each do |i| %>
          addImageLinkWithData("<%= i %>");
        <% end %>
      <% end %>
    });
  </script>

  <div class="form-group">
    <label><%= t "admin.name" %></label>
    <%= f.text_field :name, required: :required, class: 'form-control' %>
  </div>
  <div class="form-group">
    <label><%= t "admin.unit" %></label>
    <%
      unit_opts = []
      Product::UNITS.each do |u|
        unit_opts << [Product.get_unit_text(u), u]
      end
    %>
    <%= f.select(
          :unit,
          options_for_select(unit_opts, @product.unit),
          {},
          {
            class: 'form-control',
            style: 'width: 200px;'
          }) %>
  </div>
  <div class="form-group">
    <label><%= t "admin.price" %></label>
    <%= f.text_field :price, required: :required, type: :number, class: 'form-control' %>
  </div>
  <div class="form-group">
    <label><%= t "admin.quantity" %></label>
    <%= f.text_field :quantity, required: :required, type: :number, class: 'form-control' %>
  </div>
  <div class="form-group">
    <label><%= t "admin.category" %></label>
    <%
      cate_opts = []
      @categories.each do |c|
        cate_opts << [c.name, c.id]
      end
    %>
    <%= f.select(
          :category_id,
          options_for_select(cate_opts, @product.category_id),
          {},
          {
            class: 'form-control',
            style: 'width: 200px;'
          }) %>
  </div>
  <div class="form-group">
    <label><%= t "admin.desc" %></label>
    <%= f.text_area :desc, rows: 5, cols: 40, class: "ckeditor form-control" %>
  </div>
  
  <%= render partial: 'admin/product/custom_fields' %>

  <script type="text/javascript">
    $(function() {
      <% @product.custom_fields.each do |name, options| %>
        addCustomField(
          "<%= name %>",
          "<%= options.join(', ') %>",
          "<%= t 'admin.custom_field.name'%>",
          "<%= t 'admin.custom_field.options'%>");
      <% end %>
    });
  </script>

  <%= render partial: 'admin/product/tags' %>

  <script type="text/javascript">
    $(function() {
      <% @product.tags.each do |t| %>
        addTag("<%= t %>");
      <% end %>
    })
  </script>

  <%= f.submit t("admin.edit"), class: 'btn btn-warning' %>
<% end %>

<script type="text/javascript">
  productNewEdit();
</script>